package com.harboursoftware.xstorage.unit.ac

import com.harboursoftware.xstorage.ac.Grant 
import com.harboursoftware.xstorage.ac.Grantee 
import com.harboursoftware.xstorage.ac.Permission 
import com.harboursoftware.xstorage.model.XUser 


import org.junit.After;
import org.junit.Before;
import org.junit.Test 

class GrantTest {
	def user
	def mockGrantee
	def grantFullControl
	def grant
	
	@Before
	void setUp() {
		user = new XUser(id : 'a9a7b886d6fd24a52fe8ca5bef65f89a64e0193f23000e241bf9b1c61be666e9')
		def contains = {if(it.id == user.id){true} else {false} }
		def toXML = {'<Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="CanonicalUser">' +
                     '<ID>a9a7b886d6fd24a52fe8ca5bef65f89a64e0193f23000e241bf9b1c61be666e9</ID>' +
                     '<DisplayName>chriscustomer</DisplayName>' +
                     '</Grantee>'}
		def getScope = {1}
		mockGrantee = [contains : contains, toXML : toXML, getScope : getScope] as Grantee
		grantFullControl = new Grant(mockGrantee, Permission.FULL_CONTROL)
		grant = new Grant(mockGrantee, Permission.WRITE)
	}
	
	@After
	void tearDown() {
		user = null
		mockGrantee = null
		grantFullControl = null
		grant = null
		
	}
	
	@Test
	void test_checkPermisson_full_cotrol1() {
		def user1 = new XUser(id : 'a9a7b886d6fd24a52fe8ca5bef65f89a64e0193f23000e241bf9b1c61be666e9')
		assert grantFullControl.checkPermission(user1, Permission.FULL_CONTROL)
	}
	
	@Test
	void test_checkPermission_full_control2() {
		def user1 = new XUser(id : 'a9a7b886d6fd24a52fe8ca5bef65f89a64e0193f23000e241bf9b1c61be666e9')
		assert grantFullControl.checkPermission(user1, Permission.WRITE)
	}
	
	@Test
	void test_checkPermission_full_control_with_user_not_match() {
		def user1 = new XUser(id : 'sss')
		assert !grantFullControl.checkPermission(user1, Permission.FULL_CONTROL)
	}
	
	@Test
	void test_checkPermission_write1() {
		def user1 = new XUser(id : 'a9a7b886d6fd24a52fe8ca5bef65f89a64e0193f23000e241bf9b1c61be666e9')
		assert grant.checkPermission(user1, Permission.WRITE)
	}
	
	@Test
	void test_checkPermission_write2() {
		def user1 = new XUser(id : 'a9a7b886d6fd24a52fe8ca5bef65f89a64e0193f23000e241bf9b1c61be666e9')
		assert !grant.checkPermission(user1, Permission.READ)
	}
	
	
	@Test
	void test_toXML() {
		assert grantFullControl.toXML() == '<Grant><Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="CanonicalUser">' +
                                '<ID>a9a7b886d6fd24a52fe8ca5bef65f89a64e0193f23000e241bf9b1c61be666e9</ID>' +
                                '<DisplayName>chriscustomer</DisplayName>' +
                                '</Grantee>' +
                                '<Permission>FULL_CONTROL</Permission>' +
                                '</Grant>'
	}
    
    @Test
    void testEquals() {
        
    }
}
